package com.tool.image;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

import com.tool.tcp.PartOne;
import com.tool.tcp.SpriteFactory;
import org.come.Frame.BoothBoxJframe;
import org.come.Frame.StallBuyJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.ZhuJpanel;
import org.come.baitan.BaiTanJframe;
import org.come.bean.NpcInfoBean;
import org.come.model.Door;
import org.come.mouslisten.Mouselistener;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.MessagrFlagUntil;
import org.come.until.SplitStringTool;
import org.come.until.UserMessUntil;
import org.come.until.Util;

import com.tool.ModerateTask.TaskProgress;
import com.tool.Stall.StallBean;
import com.tool.btn.JpanelOnJalbelBtn;
import com.tool.imagemonitor.CreepsMonitor;
import com.tool.imagemonitor.NpcMonitor;
import com.tool.imagemonitor.PlayerMonitor;
import com.tool.imagemonitor.TaskMonitor;
import com.tool.tcpimg.UIUtils;
import come.tool.BangBattle.BangFight;
import come.tool.Scene.DNTGScene;
import come.tool.Scene.Scene;

//TODO 画图，所有图像数据，是画NPC的上级类
/**
 * 类说明
 *
 * @author 洪毅豪
 * @date 2017年12月24日 新建
 *
 *
 *       所有的图像数据
 */
public class ImageMixDeal {
	public static List<StallBean> isdianji=new ArrayList<>();
	public static ManimgAttribute userimg = null;// 玩家图像属性
	public static List<ManimgAttribute> npcimglist = new ArrayList<>();// npc图像属性;
	public static ConcurrentHashMap<String,ManimgAttribute> Playerimgmap = new ConcurrentHashMap<>();// 其他玩家图像属性
	public static String username;
	// 存定时刷新野怪
	public static List<ManimgAttribute> mapMonsterlist = new ArrayList<>();
	// 存任务怪
	public static List<ManimgAttribute> tasklist = new ArrayList<>();
	// 存地图的摊位信息
	public static List<StallBean> stalls = new ArrayList<>();
	public static Scene scene;
	// 判断是否隐藏判断
	public static boolean ISSTALL=true;
	//判断是否是影子模式
	public static boolean isShadow = true;

  //  public static Image image = CutButtonImage.getImage("inkImg/old/1/flb.png", 1920, 1080).getImage();
    
    public static ConcurrentHashMap<String,List<ManimgAttribute>> petMap = new ConcurrentHashMap<>();// 其他玩家图像属性
    
    public static boolean ismove=true;
	/**画图*/
	public static void Drawing(Graphics2D g2, long DieTime) {
		try {
			ImageMixDeal.move(DieTime);
			Util.mapmodel.drawmap(g2);
			if (scene==null) {
				// 画野怪
				for (int i = mapMonsterlist.size()-1; i >=0 ; i--) {
					mapMonsterlist.get(i).move2(DieTime);
					mapMonsterlist.get(i).Drawing(g2,DieTime);
				}
				if (tasklist.size()!=0) {//画任务怪
					for (int i = tasklist.size()-1; i >=0 ; i--) {
						if (tasklist.get(i).getTaskdata().getType()==5) {
							tasklist.get(i).GS(userimg.getmove());
							tasklist.get(i).Drawing(g2,DieTime);
						}else if (Util.ditubianma==tasklist.get(i).getTaskdata().getMap()) {
							tasklist.get(i).Drawing(g2,DieTime);
						}
					}
				}
				for (ManimgAttribute value : Playerimgmap.values()) {
					value.Drawing(g2,DieTime);
				}
				for (int i = npcimglist.size()-1; i >=0 ; i--) {
					npcimglist.get(i).Drawing(g2,DieTime);
				}
				//如果组队 不考虑跟随
//				if (ImageMixDeal.userimg.getRoleShow().getTeamInfo() == null || ImageMixDeal.userimg.getRoleShow().getTeamInfo().equals("")) {
				//宠物跟随
				for (List<ManimgAttribute> value:petMap.values()){
					for (int i=0;i<value.size();i++) {
						value.get(i).Drawing(g2,DieTime);
					}
				}
//				}
				if (ISSTALL) {
					//摊位字体颜色
					g2.setFont(UIUtils.TEXT_FONT78);
					g2.setColor(new Color(224, 224, 71));
					for (int i = stalls.size()-1; i >=0 ; i--) {
						((StallBean)ImageMixDeal.stalls.get(i)).draw((Graphics)g2,isdianji.contains(stalls.get(i)));
					}
				}
				//飞行器云雾
				/*if(userimg.getRoleShow().getFly_id()>0) {
					g2.drawImage(ImageMixDeal.image, 0, 0, null);
				}*/
				for (ManimgAttribute value : Playerimgmap.values()) {
					if(value.getRoleShow().getFly_id()>0) {
						value.Drawing(g2,DieTime);
					}
				}
				userimg.Drawing(g2,DieTime);
				userimg.draweffects(g2);
				BangFight.getBangFight().draw(g2);
			}else {
				scene.draw(g2, DieTime);
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}
	}
	/**
	 * move
	 */
	static String T=",";
	static StringBuffer buffer=new StringBuffer();
	public static void move(long DieTime) {
		for (ManimgAttribute value:Playerimgmap.values()) {
			/*if(Util.ZOU) {
				value.movezou(DieTime);
			}else {*/
				value.move(DieTime);
			//}
		}
		//如果组队 不考虑跟随
//		if (ImageMixDeal.userimg.getRoleShow().getTeamInfo() == null || ImageMixDeal.userimg.getRoleShow().getTeamInfo().equals("")) {
		//宠物跟随
		petMap.forEach((k,v)->{
			ManimgAttribute manimgAttribute = ImageMixDeal.huoquLogin(k);
			if (manimgAttribute!=null){
				manimgAttribute.movePet(v);
			}
		});
//		}

		if (userimg != null) {
			/*if(Util.ZOU) {
				userimg.movezou(DieTime);
			}else {*/
				userimg.move(DieTime);
		//	}
			if (userimg.getType() == 1) {
				buffer.setLength(0);
				buffer.append(userimg.getRoleShow().getX()/20);
				buffer.append(T);
				buffer.append(userimg.getRoleShow().getY()/20);
				ZhuJpanel.getxAndY().setText(Util.mapName +"   [ "+ buffer.toString()+" ]");
				Util.mapmodel.ShotMove(userimg.getRoleShow().getX(),userimg.getRoleShow().getY());
				if (Util.CREEPSMAP) {
					// 判断移动中是否会碰到野怪
					if (userimg.getTeams()!=null){
						Util.GotoCreeps();
					}
				}
			}else {
				Util.mapmodel.ShotMiddlex(userimg.getRoleShow().getX());
				Util.mapmodel.ShotMiddley(userimg.getRoleShow().getY());
			}
		}

	}
	/**move*/
	public static void move2(long DieTime) {
		for (int i = mapMonsterlist.size()-1; i >=0 ; i--) {
//			mapMonsterlist.get(i).move2(DieTime);
		}
	}

	static List<String> strings;
	static NpcInfoBean infoBean;
	static List<Door> doors;
	static List<String> strings1;

	/**加载npc*/
	public static void NpcLoad(String npc) {
		npcimglist.clear();
		if (npc != null) {
			strings = SplitStringTool.splitString(npc);
			for (int i = 0; i < strings.size(); i++) {
				infoBean = UserMessUntil.getnpc(strings.get(i));
				if (infoBean == null) {
					continue;
				}
				if ("2".equals(infoBean.getNpctable().getNpctype())) {
					doors = new ArrayList<>();
					if (infoBean.getNpctable().getNpcway() != null) {
						strings1 = SplitStringTool.splitString(infoBean.getNpctable().getNpcway());
						for (int j = 0; j < strings1.size(); j++) {
							doors.add(UserMessUntil.getDoor(strings1.get(j)));
						}
					}
					infoBean.setDoors(doors);
				}
				npcimglist.add(new ManimgAttribute(infoBean));
			}
		}
	}

	/**获取指定名字的数据*/
	public static ManimgAttribute huoquLogin(String name) {
		if (name.equals(username)) {
			return userimg;
		}
		return Playerimgmap.get(name);
	}
	/**获取指定id的数据*/
	public static ManimgAttribute huoquLogin(BigDecimal id) {
		if (userimg.getRoleShow().getRole_id().compareTo(id)==0){
			return userimg;
		}
		try {
			for (ManimgAttribute value : Playerimgmap.values()) {
				if (value.getRoleShow().getRole_id().compareTo(id)==0) {
					return value;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	static ManimgAttribute attribute = null;

	static PartOne part;
	//TODO 鼠标悬停NPC名字变色（非战斗）
	/**
	 * 判断是否移动到了实体上，包括玩家、NPC、怪物，如果移动到了非自身单位则其他单位名字变色
	 */
	public static void colorName(int x, int y)
	{
		if (scene!=null&&scene.Monitor(x, y)) {
			return;
		}

		if (ImageMixDeal.userimg.getTeams()!=null) {
			try {
				ManimgAttribute attribute = null;
				// npc
				for (int i = 0; i < npcimglist.size(); i++) {
					attribute = npcimglist.get(i);
					String npcSkin = attribute.getNpc().getNpctable().getSkin();
					try {
						part = (PartOne) attribute.getPart();
					} catch (Exception e) {
					}
					if (attribute.isContains(x, y)) {
						attribute.setPart(SpriteFactory.createPart(part.getSkin(), 2, 1, "gl"));
						attribute.setNameColor(UIUtils.COLOR_NPC_SELECT);
						if (Util.hideTitle && MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE1) && !"4004".equals(npcSkin)) {
							MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE13);
						}
						return;
					} else {
						if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE12) || MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE13)) {
							MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
						}
						attribute.setDefaultNameColor();
//						PartOne partHighlight = (PartOne) SpriteFactory.createPart(part.getSkin(), 2, 1, "");
						attribute.setPart(SpriteFactory.createPart(part.getSkin(), 2, 1, null));
					}
				}
				// 任务怪
				for (int i = 0; i < tasklist.size(); i++) {
					attribute = tasklist.get(i);
					part = (PartOne) attribute.getPart();
					if (attribute.isContains(x, y)) {
						attribute.setPart(SpriteFactory.createPart(part.getSkin(), 2, 1, "gl"));
						attribute.setNameColor(UIUtils.COLOR_NPC_SELECT);
						if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE1)) {
							MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
						}
						return;
					} else {
						if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE12)) {
							MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
						}
						attribute.setDefaultNameColor();
//						PartOne partHighlight = (PartOne) SpriteFactory.createPart(part.getSkin(), 2, 1, "");
						attribute.setPart(SpriteFactory.createPart(part.getSkin(), 2, 1, null));
					}
				}
				// 野怪
				for (int i = 0; i < mapMonsterlist.size(); i++) {
					attribute = mapMonsterlist.get(i);
					try {
						part = (PartOne) attribute.getPart();
					} catch (Exception e) {
//						e.printStackTrace();
					}
					//假人相关
//					if(attribute.getMapMonsterBean().getRobottitle()!=null && !attribute.getMapMonsterBean().getRobottitle().equals("")) {

//					}else {
					if (attribute.isContains(x, y)) {
						attribute.setPart(SpriteFactory.createPart(part.getSkin(), 2, 1, "gl"));
						attribute.setNameColor(UIUtils.COLOR_NPC_SELECT);
						if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE1)) {
							MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
						}
						return;
					} else {
						if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE12)) {
							MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
						}
						attribute.setDefaultNameColor();
//						PartOne partHighlight = (PartOne) SpriteFactory.createPart(part.getSkin(), 2, 1, "");
						attribute.setPart(SpriteFactory.createPart(part.getSkin(), 2, 1, null));
					}
//					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			//帮战建筑物监听
//			if (BangFight.getBangFight().Monitor(x,y)){
//				return;
//			}
		}

		/**玩家监听*/
		if (userimg.isContains(x,y)) {
			if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE1)) {
				MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
			}
//			PlayerMonitor.Player(userimg);
			////system.out.println("玩家");
			return;
		}
		try {
			for (ManimgAttribute value : Playerimgmap.values()) {
				if (value.isContains(x, y)) {
					if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE1)) {
						MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);
					}
					//PlayerMonitor.Player(value);
					////system.out.println("其他玩家");
					return;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

//		/**玩家监听*/
//		if (userimg.isContains(x,y)) {
////			PlayerMonitor.Player(userimg);
//			////system.out.println("玩家");
//			return;
//		}
//		try
//		{
//			for (ManimgAttribute value : Playerimgmap.values()) {
//				PartOne part = (PartOne) value.getPart();
//				if (value.isContains(x, y)) {
//					value.setNameColor(UIUtils.COLOR_NPC_SELECT);
//					value.setPart(SpriteFactory.createPart(part.getSkin(), 2, 1, "gl"));
//					//PlayerMonitor.Player(value);
//					////system.out.println("其他玩家");
//					return;
//				} else {
//					value.setDefaultNameColor();
//					PartOne partHighlight = (PartOne) SpriteFactory.createPart(part.getSkin(), 2, 1, "");
//					value.setPart(SpriteFactory.createPart(part.getSkin(), 2, 1, null));
//				}
//			}
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	/**
	 * 判断是否点击到人物上 监听触发
	 */
	public static void MonitorTrigger(int x, int y) {
		// 触发次序 先npc 野怪 玩家自己 其他玩家
		if (scene!=null&&scene.Monitor(x, y)) {
			Util.dj = true;
			return;
		}
		double x1=userimg.getRoleShow().getX();
		double y1=userimg.getRoleShow().getY();
		int  distance = (int) Math.sqrt(Math.pow(x - x1, 2) + Math.pow(y - y1, 2));
		System.out.println(distance);
		System.out.println(x1);
		System.out.println(y1);
		if (distance>=550){
			ZhuFrame.getZhuJpanel().addPrompt2("距离太远了#35");
			return;
		}
		if (ImageMixDeal.userimg.getTeams()!=null) {
			try {
				if (ISSTALL) {
					for (int i = stalls.size()-1; i >=0 ; i--) {
						StallBean bean=stalls.get(i);
						if (bean.isDJ(x, y)) {
							if (bean.getRoleid().compareTo(ImageMixDeal.userimg.getRoleShow().getRole_id())==0) {
								JpanelOnJalbelBtn.stall();
							}else {
								Util.dj = true;
								//显示购买窗口
								//FormsManagement.showForm(35);
								//发送id
								isdianji.add(bean);
								final String sendMes = Agreement.getAgreement().stallgetAgreement(bean.getId() + "");
								SendMessageUntil.toServer(sendMes);
							}
							return;
						}
					}
				}
				// npc
				for (int i = 0; i < npcimglist.size(); i++) {
					attribute = npcimglist.get(i);
					if (attribute.isContains(x, y)) {
						NpcMonitor.npc(attribute);
						Util.dj = true;
						return;
					}
				}
				// 任务怪
				for (int i = 0; i < tasklist.size(); i++) {
					attribute = tasklist.get(i);
					if (attribute.isContains(x, y)) {
						TaskMonitor.TaskCreeps(attribute.getTaskdata());
						Util.dj = true;
						return;
					}
				}
				// 野怪
				for (int i = 0; i < mapMonsterlist.size(); i++) {
					attribute = mapMonsterlist.get(i);
					if (attribute.isContains(x, y)) {
						if(attribute.getMapMonsterBean()!=null && attribute.getMapMonsterBean().getRobottitle()!=null && !"".equals(attribute.getMapMonsterBean().getRobottitle())) {
							return;
						}else {
							Util.dj = true;
							CreepsMonitor.Creeps(attribute);
							return;
						}
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			//帮战建筑物监听
			if (BangFight.getBangFight().Monitor(x,y)){
				Util.dj = true;
				return;
			}

		}
		/**玩家监听*/
		if (userimg.isContains(x,y)) {
			PlayerMonitor.Player(userimg);
			return;
		}
		try {
			for (ManimgAttribute value : Playerimgmap.values()) {
				if (value.isContains(x, y)) {

					PlayerMonitor.Player(value);
					Util.dj = true;
					return;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		Util.dj=false;
	}

	/**
	 * 图像监听 根据色素
	 */
	public static boolean toBufferedImage(Image image, int x, int y) {
		if (image == null) {
			return false;
		}
		if ((x >= 0 && x < image.getWidth(null))
				&& (y >= 0 && y < image.getHeight(null))) {
			BufferedImage bimage = null;
			if (image instanceof BufferedImage) {
				bimage = (BufferedImage) image;
			} else {
				return true;
			}
			return bimage.getRGB(x, y) != 0;
		}
		return false;
	}

	/**添加野怪*/
	public static void addMonster(TaskProgress progress) {
		if (progress.getSum()>=progress.getMax()){return;}
		if (progress.getType()==1||progress.getType()==5) {
			tasklist.add(new ManimgAttribute(progress));
		}
	}
	/**删除野怪*/
	public static void removeMonster(TaskProgress progress) {
		if (progress.getType()==1||progress.getType()==5) {
			for (int i = 0; i < tasklist.size(); i++) {
				if (tasklist.get(i).getName().equals(progress.getDName())) {
					tasklist.remove(i);
					return;
				}
			}
		}
	}
	/**副本信息展示*/
	public static void upScene(String sceneMsg){
		if (sceneMsg==null|| "".equals(sceneMsg)) {
			return;
		}
		String[] vs=sceneMsg.split("\\|");
		int sceneId=Integer.parseInt(vs[0]);
		if (vs.length==1) {
			if (scene!=null&&scene.getSceneId()==sceneId) {
				scene.end();
				scene=null;
			}
		}else if (scene!=null) {
			if (scene.getSceneId()==sceneId) {
				scene.UPData(vs);
			}else {
				scene.end();
				scene=null;
				initScene(sceneId, vs);
			}
		}else {
			initScene(sceneId, vs);
		}
	}
	/**初始化副本*/
	public static void initScene(int sceneId,String[] vs){
		if (sceneId==Scene.DNTGID) {
			scene=new DNTGScene(vs);
		}
	}

	//*摊位处理
	public static void stall(StallBean bean){
		if (bean.getState()==StallBean.NO) {//关门
			StallBuyJframe.getStallBuyJframe().getStallBuyJpanel().no(bean);
			ManimgAttribute attribute=huoquLogin(bean.getRole());
			if (attribute!=null) {
				attribute.getRoleShow().setBooth_id(null);
				if (attribute==userimg) {
					//重置摆摊界面
					BaiTanJframe.getBaiTanJframe().getBaiTanJpanel().CId(bean);
				}
			}
			for (int i = stalls.size()-1; i >=0; i--) {
				if (stalls.get(i).getId()==bean.getId()) {
					stalls.remove(i);
					return;
				}
			}
		}else {
			ManimgAttribute attribute=huoquLogin(bean.getRole());
			if (attribute!=null) {
				attribute.getRoleShow().setBooth_id(new BigDecimal(bean.getId()));
				if (attribute==userimg) {
					//重置摆摊界面
					BaiTanJframe.getBaiTanJframe().getBaiTanJpanel().CId(bean);
				}
			}
			for (int i = stalls.size()-1; i >=0; i--) {
				if (stalls.get(i).getId()==bean.getId()) {
					stalls.set(i, bean);
					return;
				}
			}
			stalls.add(bean);
		}
	}
	/***/
	public static ManimgAttribute getNpc(String npc){
		for (int i = 0; i < npcimglist.size(); i++) {
			ManimgAttribute attribute = npcimglist.get(i);
			if (attribute.getNpc().getNpctable().getNpcid().equals(npc)) {
				return attribute;
			}
		}
		return null;
	}
	public static ManimgAttribute getTask(int id){
		for (int i = 0; i < tasklist.size(); i++) {
			ManimgAttribute attribute = tasklist.get(i);
			if (attribute.getTaskdata().getDId()==id) {
				return attribute;
			}
		}
		return null;
	}
	public static ManimgAttribute getMonster(int id){
		for (int i = mapMonsterlist.size()-1; i >=0 ; i--) {
			if (id==mapMonsterlist.get(i).getMapMonsterBean().getI()){
				return mapMonsterlist.get(i);
			}
		}
		return null;
	}
	public static void removeMonster(int id){
		for (int i = mapMonsterlist.size()-1; i >=0 ; i--) {
			if (id==mapMonsterlist.get(i).getMapMonsterBean().getI()){
				mapMonsterlist.remove(i);
				return;
			}
		}
	}
}
